package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.dto.ChatGroupMemberSettingDTO;
import com.zenithmind.chat.pojo.vo.ChatGroupMemberSettingVO;
import com.zenithmind.chat.service.ChatGroupMemberSettingService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 群组成员设置控制器 - 遵循单一职责原则
 * 只负责群组成员设置的HTTP接口处理
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/chat/group-member-settings")
@RequiredArgsConstructor
@Validated
@Tag(name = "群组成员设置管理", description = "群组成员设置相关接口")
public class ChatGroupMemberSettingController {

    private final ChatGroupMemberSettingService chatGroupMemberSettingService;

    @GetMapping("/{groupId}/{userId}")
    @Operation(summary = "查询成员设置", description = "根据群组ID和用户ID查询成员设置")
    public Result<ChatGroupMemberSettingVO> getMemberSetting(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        ChatGroupMemberSettingVO setting = chatGroupMemberSettingService.getMemberSetting(groupId, userId);
        return Result.success(setting);
    }

    @PutMapping
    @Operation(summary = "更新成员设置", description = "更新群组成员设置信息")
    public Result<Boolean> updateMemberSetting(@Valid @RequestBody ChatGroupMemberSettingDTO dto) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.updateMemberSetting(dto, operatorId);
        return Result.success(result);
    }

    @PostMapping("/{groupId}/{userId}/default")
    @Operation(summary = "创建默认设置", description = "为群组成员创建默认设置")
    public Result<String> createDefaultSetting(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        String operatorId = UserContext.getUserId();
        String settingId = chatGroupMemberSettingService.createDefaultSetting(groupId, userId, operatorId);
        return Result.success(settingId);
    }

    @DeleteMapping("/{groupId}/{userId}")
    @Operation(summary = "删除成员设置", description = "删除指定群组成员的设置")
    public Result<Boolean> deleteMemberSetting(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.deleteMemberSetting(groupId, userId, operatorId);
        return Result.success(result);
    }

    @GetMapping("/group/{groupId}")
    @Operation(summary = "查询群组成员设置", description = "查询指定群组的所有成员设置")
    public Result<List<ChatGroupMemberSettingVO>> getGroupMemberSettings(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        List<ChatGroupMemberSettingVO> settings = chatGroupMemberSettingService.getGroupMemberSettings(groupId);
        return Result.success(settings);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "查询用户群组设置", description = "查询指定用户的所有群组设置")
    public Result<List<ChatGroupMemberSettingVO>> getUserGroupSettings(
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        List<ChatGroupMemberSettingVO> settings = chatGroupMemberSettingService.getUserGroupSettings(userId);
        return Result.success(settings);
    }

    @PutMapping("/{groupId}/{userId}/nickname")
    @Operation(summary = "更新成员昵称", description = "更新群组成员的昵称")
    public Result<Boolean> updateMemberNickname(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId,
            @Parameter(description = "昵称") @RequestParam String nickname) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.updateMemberNickname(groupId, userId, nickname, operatorId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/{userId}/top-status")
    @Operation(summary = "更新置顶状态", description = "更新群组的置顶状态")
    public Result<Boolean> updateTopStatus(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId,
            @Parameter(description = "是否置顶") @RequestParam Boolean isTop) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.updateTopStatus(groupId, userId, isTop, operatorId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/{userId}/mute-status")
    @Operation(summary = "更新免打扰状态", description = "更新群组的免打扰状态")
    public Result<Boolean> updateMuteStatus(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId,
            @Parameter(description = "是否免打扰") @RequestParam Boolean isMuted) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.updateMuteStatus(groupId, userId, isMuted, operatorId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/{userId}/message-notification")
    @Operation(summary = "更新消息提醒设置", description = "更新群组的消息提醒设置")
    public Result<Boolean> updateMessageNotification(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId,
            @Parameter(description = "消息提醒设置") @RequestParam Integer messageNotification) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.updateMessageNotification(groupId, userId, messageNotification, operatorId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/{userId}/show-nickname")
    @Operation(summary = "更新显示昵称设置", description = "更新是否显示成员昵称的设置")
    public Result<Boolean> updateShowMemberNickname(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId,
            @Parameter(description = "是否显示成员昵称") @RequestParam Boolean showMemberNickname) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.updateShowMemberNickname(groupId, userId, showMemberNickname, operatorId);
        return Result.success(result);
    }

    @PutMapping("/{groupId}/{userId}/save-contacts")
    @Operation(summary = "更新通讯录设置", description = "更新是否保存到通讯录的设置")
    public Result<Boolean> updateSaveToContacts(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId,
            @Parameter(description = "是否保存到通讯录") @RequestParam Boolean saveToContacts) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.updateSaveToContacts(groupId, userId, saveToContacts, operatorId);
        return Result.success(result);
    }

    @GetMapping("/user/{userId}/top-groups")
    @Operation(summary = "查询置顶群组", description = "查询用户的置顶群组列表")
    public Result<List<ChatGroupMemberSettingVO>> getTopGroups(
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        List<ChatGroupMemberSettingVO> topGroups = chatGroupMemberSettingService.getTopGroups(userId);
        return Result.success(topGroups);
    }

    @GetMapping("/user/{userId}/muted-groups")
    @Operation(summary = "查询免打扰群组", description = "查询用户的免打扰群组列表")
    public Result<List<ChatGroupMemberSettingVO>> getMutedGroups(
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        List<ChatGroupMemberSettingVO> mutedGroups = chatGroupMemberSettingService.getMutedGroups(userId);
        return Result.success(mutedGroups);
    }

    @GetMapping("/group/{groupId}/top-members")
    @Operation(summary = "查询置顶成员", description = "查询群组中的置顶成员列表")
    public Result<List<ChatGroupMemberSettingVO>> getTopMembers(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        List<ChatGroupMemberSettingVO> topMembers = chatGroupMemberSettingService.getTopMembers(groupId);
        return Result.success(topMembers);
    }

    @GetMapping("/group/{groupId}/muted-members")
    @Operation(summary = "查询免打扰成员", description = "查询群组中的免打扰成员列表")
    public Result<List<ChatGroupMemberSettingVO>> getMutedMembers(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        List<ChatGroupMemberSettingVO> mutedMembers = chatGroupMemberSettingService.getMutedMembers(groupId);
        return Result.success(mutedMembers);
    }

    @GetMapping("/check-exists/{groupId}/{userId}")
    @Operation(summary = "检查设置是否存在", description = "检查指定群组成员的设置是否存在")
    public Result<Boolean> checkSettingExists(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        Boolean exists = chatGroupMemberSettingService.checkSettingExists(groupId, userId);
        return Result.success(exists);
    }

    @GetMapping("/user/{userId}/count/top-groups")
    @Operation(summary = "统计置顶群组数量", description = "统计用户的置顶群组数量")
    public Result<Integer> countTopGroups(
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        Integer count = chatGroupMemberSettingService.countTopGroups(userId);
        return Result.success(count);
    }

    @GetMapping("/user/{userId}/count/muted-groups")
    @Operation(summary = "统计免打扰群组数量", description = "统计用户的免打扰群组数量")
    public Result<Integer> countMutedGroups(
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        Integer count = chatGroupMemberSettingService.countMutedGroups(userId);
        return Result.success(count);
    }

    @GetMapping("/group/{groupId}/count/top-members")
    @Operation(summary = "统计置顶成员数量", description = "统计群组中的置顶成员数量")
    public Result<Integer> countTopMembers(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        Integer count = chatGroupMemberSettingService.countTopMembers(groupId);
        return Result.success(count);
    }

    @GetMapping("/group/{groupId}/count/muted-members")
    @Operation(summary = "统计免打扰成员数量", description = "统计群组中的免打扰成员数量")
    public Result<Integer> countMutedMembers(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        Integer count = chatGroupMemberSettingService.countMutedMembers(groupId);
        return Result.success(count);
    }

    @DeleteMapping("/group/{groupId}/batch")
    @Operation(summary = "批量删除群组设置", description = "批量删除群组的所有成员设置")
    public Result<Integer> batchDeleteByGroupId(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        String operatorId = UserContext.getUserId();
        Integer deletedCount = chatGroupMemberSettingService.batchDeleteByGroupId(groupId, operatorId);
        return Result.success(deletedCount);
    }

    @DeleteMapping("/user/{userId}/batch")
    @Operation(summary = "批量删除用户设置", description = "批量删除用户的所有群组设置")
    public Result<Integer> batchDeleteByUserId(
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        String operatorId = UserContext.getUserId();
        Integer deletedCount = chatGroupMemberSettingService.batchDeleteByUserId(userId, operatorId);
        return Result.success(deletedCount);
    }

    @PutMapping("/group/{groupId}/batch")
    @Operation(summary = "批量更新成员设置", description = "批量更新群组成员的设置")
    public Result<Boolean> batchUpdateMemberSettings(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @RequestBody @NotEmpty List<String> userIds,
            @Valid @RequestBody ChatGroupMemberSettingDTO dto) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.batchUpdateMemberSettings(groupId, userIds, dto, operatorId);
        return Result.success(result);
    }

    @PostMapping("/copy")
    @Operation(summary = "复制成员设置", description = "复制成员设置到其他群组或用户")
    public Result<Boolean> copyMemberSetting(
            @Parameter(description = "源群组ID") @RequestParam @NotBlank String sourceGroupId,
            @Parameter(description = "源用户ID") @RequestParam @NotBlank String sourceUserId,
            @Parameter(description = "目标群组ID") @RequestParam @NotBlank String targetGroupId,
            @Parameter(description = "目标用户ID") @RequestParam @NotBlank String targetUserId) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.copyMemberSetting(
                sourceGroupId, sourceUserId, targetGroupId, targetUserId, operatorId);
        return Result.success(result);
    }

    @PostMapping("/{groupId}/{userId}/reset")
    @Operation(summary = "重置为默认设置", description = "将成员设置重置为默认值")
    public Result<Boolean> resetToDefault(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.resetToDefault(groupId, userId, operatorId);
        return Result.success(result);
    }

    @GetMapping("/group/{groupId}/export")
    @Operation(summary = "导出成员设置", description = "导出群组的成员设置为文件")
    public Result<String> exportMemberSettings(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        String operatorId = UserContext.getUserId();
        String exportPath = chatGroupMemberSettingService.exportMemberSettings(groupId, operatorId);
        return Result.success(exportPath);
    }

    @PostMapping("/group/{groupId}/import")
    @Operation(summary = "导入成员设置", description = "从文件导入群组的成员设置")
    public Result<Boolean> importMemberSettings(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "文件路径") @RequestParam @NotBlank String filePath) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.importMemberSettings(groupId, filePath, operatorId);
        return Result.success(result);
    }

    @GetMapping("/group/{groupId}/statistics")
    @Operation(summary = "获取设置统计", description = "获取群组成员设置的统计信息")
    public Result<Object> getMemberSettingStatistics(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId) {
        Object statistics = chatGroupMemberSettingService.getMemberSettingStatistics(groupId);
        return Result.success(statistics);
    }

    @PostMapping("/{groupId}/{userId}/sync")
    @Operation(summary = "同步成员设置", description = "同步群组成员的设置信息")
    public Result<Boolean> syncMemberSetting(
            @Parameter(description = "群组ID") @PathVariable @NotBlank String groupId,
            @Parameter(description = "用户ID") @PathVariable @NotBlank String userId) {
        String operatorId = UserContext.getUserId();
        Boolean result = chatGroupMemberSettingService.syncMemberSetting(groupId, userId, operatorId);
        return Result.success(result);
    }
}
